﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;

namespace BladeTecWebApplication.Admin
{
	public partial class DefineQuestion : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				NetSysData.NetSysData data = new NetSysData.NetSysData();
				QUESTION_LEVEL[] levels = data.GetQuestionLevels();
				QUESTION_CATEGORY[] categories = data.GetQuestionCagetories();

				ddlLevel.Items.Add(new ListItem("Select ...", ""));
				ddlCategory.Items.Add(new ListItem("Select ...", ""));

				foreach (QUESTION_LEVEL level in levels)
				{
					ddlLevel.Items.Add(new ListItem(level.LEVEL_DESCRIPTION, level.LEVEL_ID.ToString()));
				}

				foreach (QUESTION_CATEGORY category in categories)
				{
					ddlCategory.Items.Add(new ListItem(category.CATEGORY_NAME, category.CATEGORY_ID.ToString()));
				}
				pnlSuccessMessage.Visible = false;
				rblQuestionType.SelectedIndex = 0;
			}
		}

		protected void btnAddQuestion_Click(object sender, EventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			QUESTION question = new QUESTION();

			question.QUESTION_TEXT = txtQuestionText.Text;
			question.LEVEL_ID = Int32.Parse(ddlLevel.SelectedValue);
			question.QUESTION_TYPE = rblQuestionType.SelectedValue;
			question.CATEGORY_ID = Int32.Parse(ddlCategory.SelectedValue);

			if (rblQuestionType.SelectedValue == "M")
			{
				char correct = Request.Form["correct_answer"].ToArray()[0];
				for (int i = 0; i < 10; i++)
				{
					char c = (char)(i + 97);
					string answerText = Request.Form["answer_" + c];

					if (answerText == null)
					{
						continue;
					}

					if (answerText == "")
					{
						pnlErrorEmptyAnswer.Visible = true;
						return;
					}

					ANSWER answer = new ANSWER();
					answer.ANSWER_TEXT = answerText;
					answer.ANSWER_PRIORITY = 0;

					if (c == correct)
					{
						answer.IS_CORRECT_ANSWER = 1;
					}
					else
					{
						answer.IS_CORRECT_ANSWER = 0;
					}

					question.ANSWERs.Add(answer);

				}
			}

			data.AddQuestion(question);

			pnlSuccessMessage.Visible = true;
			pnlErrorEmptyAnswer.Visible = false;
			txtQuestionText.Text = "";
			ddlLevel.ClearSelection();
			ddlCategory.ClearSelection();
			rblQuestionType.SelectedIndex = 0;

		}
	}
}